/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nhncorp.neptune.client;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import com.nhncorp.neptune.common.io.NWritable;
import com.nhncorp.neptune.common.io.NWritableUtils;

/**
 * RowFilter and CellFilter are normally used to get or scan for complex search condition<BR>
 * @see RowFilter
 */
public class CellFilter implements NWritable {
  private String columnName;

  private Cell.Key cellKey;
  
  //if startTimestamp, endTimestamp is 0, scan lastest value
  //if startTimestamp = 0, endTimestamp = Long.MAX, scan all values 
  private long startTimestamp = 0;
  private long endTimestamp = 0;
  
  /**
   * Number of Cell in a single query<BR>
   * This variable is used for fetching TopN datas in a Row
   */
  private int numOfValues = 0;
  
  /**
   * Number of version in a Cell 
   */
  private int numOfVersions = 1;
  
  public CellFilter() {
    columnName = "";
    cellKey = Cell.Key.EMPTY_KEY;
  }

  public CellFilter(String columnName) {
    this.columnName = columnName;
    cellKey = Cell.Key.EMPTY_KEY;
  }
  
  public CellFilter(String columnName, Cell.Key cellKey) {
    this.columnName = columnName;
    this.cellKey = cellKey;
    if(cellKey == null) {
      this.cellKey = Cell.Key.EMPTY_KEY;
    }
  }
  
  public void readFields(DataInput in) throws IOException {
    columnName = NWritableUtils.readString(in);
    cellKey = new Cell.Key();
    cellKey.readFields(in);
    if(cellKey.isEmpty()) {
      cellKey = null;
    }
    numOfVersions = in.readInt();
    numOfValues = in.readInt();
    startTimestamp = in.readLong();
    endTimestamp = in.readLong();
  }
  
  public void write(DataOutput out) throws IOException {
    NWritableUtils.writeString(out, columnName);
    if(cellKey == null) {
      cellKey = Cell.Key.EMPTY_KEY;
    }
    cellKey.write(out);
    out.writeInt(numOfVersions);
    out.writeInt(numOfValues);
    out.writeLong(startTimestamp);
    out.writeLong(endTimestamp);
  }

  public Cell.Key getCellKey() {
    return cellKey;
  }

  public void setCellKey(Cell.Key cellKey) {
    this.cellKey = cellKey;
  }

  public void setColumn(String columnName, Cell.Key cellKey) {
    this.columnName = columnName;
    this.cellKey = cellKey;
  }

  public String getColumnName() {
    return columnName;
  }

  public long getEndTimestamp() {
    return endTimestamp;
  }

  public void setEndTimestamp(long endTimestamp) {
    this.endTimestamp = endTimestamp;
  }

  public int getNumOfVersions() {
    return numOfVersions;
  }

  /**
   * Set number of version in a Cell
   * @param numOfVersions
   */
  public void setNumOfVersions(int numOfVersions) {
    this.numOfVersions = numOfVersions;
  }

  public long getStartTimestamp() {
    return startTimestamp;
  }

  public void setStartTimestamp(long startTimestamp) {
    this.startTimestamp = startTimestamp;
  }
  
  public boolean hasTimestampFilter() {
    return startTimestamp != 0 || endTimestamp != 0; 
  }

  public int getNumOfValues() {
    return numOfValues;
  }

  /**
   * Set number of Cell in a single query<BR>
   * This variable is used for fetching TopN datas in a Row.
   * @param numOfValues
   */
  public void setNumOfValues(int numOfValues) {
    this.numOfValues = numOfValues;
  }

  public void setColumnName(String columnName) {
    this.columnName = columnName;
  }
  
  public String toString() {
    return "CellFilter:columnName=" + columnName + ", cellKey=" + cellKey + ", time=" + startTimestamp + "," + endTimestamp; 
  }
}